//
//  SampuxAddEquipmentView.m
//  Sampux
//
//  Created by rzm on 16/7/6.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxAddEquipmentView.h"
#import "SPSetProgramInfo.h"
@interface SampuxAddEquipmentView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *scanCodeBtn;
@property (nonatomic, strong) UIButton *nextStepBtn;

@end

@implementation SampuxAddEquipmentView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 12.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 12.;
    }else if (IS_IPHONE_6)
    {
        font = 14.;
    }else
    {
        font = 16.;
    }
    
    _addEquipmentView = [[SPDeleteTextFiledView alloc]initWithFrame:CGRectMake(self.width/2 - Sampux6WScale(SampuxCustomW)/2, Sampux6WScale(60) + 64, Sampux6WScale(SampuxCustomW), Sampux6HScale(SampuxCustomH))];
    [self addSubview:_addEquipmentView];
    [_addEquipmentView createDeleteTextFiledWithPlaceholder:@"输入设备ID" andLeftImage:@"add_device_icon" andRightImage:@"delete"];
    _addEquipmentView.textFiled.delegate = self;
    _addEquipmentView.textFiled.keyboardType = UIKeyboardTypeASCIICapable;
    
    _addEquipmentTextField = [UITextField createTextFieldWithRect:CGRectMake(self.width/2 - Sampux6WScale(SampuxCustomW)/2, Sampux6WScale(60) + 64, Sampux6WScale(SampuxCustomW), Sampux6HScale(SampuxCustomH)) andPlaceholder:@"输入设备ID" andLeftImage:@"addEquipment"];
//    [self addSubview:_addEquipmentTextField];
//    [UIView createLineViewInbottom:_addEquipmentTextField];
//    _addEquipmentTextField.delegate = self;
    
//    _scanCodeBtn = [UIButton createButtonWithTitle:@"点击扫描二维码" frame:CGRectMake(_addEquipmentTextField.right - Sampux6WScale(120), _addEquipmentView.bottom + SampuxMargin, Sampux6WScale(120), 40) titleFont:THFont(font) titleColor:THDeFaultBlueColor];
//    [self addSubview:_scanCodeBtn];
//    _scanCodeBtn.hitTestEdgeInsets = UIEdgeInsetsMake(- 10, -10, -10, -10);
//    [_scanCodeBtn addTarget:self action:@selector(setScanCodeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _nextStepBtn = [UIButton createButtonWithTitle:@"下一步" frame: CGRectMake(self.width/2 - Sampux6WScale(345/2)/2, _addEquipmentView.bottom + Sampux6HScale(30) , Sampux6WScale(345/2), Sampux6HScale(SampuxCustomH)) titleFont:THFont(font) titleColor:THDeFaultBlueColor];
    [self addSubview:_nextStepBtn];
    [_nextStepBtn setCorner:_nextStepBtn.height/2];
    _nextStepBtn.layer.borderWidth = 1;
    _nextStepBtn.layer.borderColor = THWhiteAlplaColor(0.6).CGColor;
    [_nextStepBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)setScanCodeBtnClick
{
    if (self.scanBl) {
        self.scanBl();
    }
}

- (void)confirmBtnClick
{
  NSString *upStr = _addEquipmentView.textFiled.text.uppercaseString;
    
//  NSLog(@"Thirty_SixBinarySystemToDecimalSystem === %d", [SPSetProgramInfo Thirty_SixBinarySystemToDecimalSystem:[upStr substringToIndex:3]]);
    
    if ([upStr isEqualToString:@""]||upStr.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"输入序列号"];
        //        [MBProgressHUD showError:@"请输入序列号"];
    } else if ([SPSetProgramInfo Thirty_SixBinarySystemToDecimalSystem:[upStr substringToIndex:3]] == DEVICE_TYPE_AMPUX_GM8_13001 &&upStr.length == 16)
    {
        if (self.nextStepBl) {
            self.nextStepBl(upStr);
        }
    }else
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的ID序列号"];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (toBeString.length > 16 && range.length!=1){
//        [MBProgressHUD showError:@"请输入正确的设备ID"];
        textField.text = [toBeString substringToIndex:16];
        return NO;
    }
    return YES;
//    if (range.location> 32) {
//        [MBProgressHUD showError:@"请输入正确的设备ID"];
//        return NO;
//    }else
//    {
//        return YES;
//    }
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self layoutIfNeeded];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight );
    return YES;
}

- (void)setNumberString:(NSString *)numberString
{
    _numberString = numberString;
    self.addEquipmentView.textFiled.text = _numberString;
}
@end
